/**
 * Generate code from /{{projectName}}-core/src/main/java/{{packageName}}/{{apis}}/rest/Abstract{{apiEntities}}Resource.java.hbs
 */
package cn.ibizlab.api.rest;

import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.ApiOperation;
import cn.ibizlab.util.domain.ImportResult;
import cn.ibizlab.api.dto.*;
import cn.ibizlab.api.mapping.*;
import cn.ibizlab.core.open.domain.OpenAccess;
import cn.ibizlab.core.open.service.OpenAccessService;
import cn.ibizlab.core.open.filter.OpenAccessSearchContext;
import cn.ibizlab.util.annotation.VersionCheck;

/**
 * 实体[OpenAccess] rest实现
 *
 * @author generator
 */
@Slf4j
public abstract class AbstractOpenAccessResource {

    @Autowired
    public OpenAccessService openAccessService;

    @Autowired
    @Lazy
    public OpenAccessDTOMapping openAccessDtoMapping;

    @Autowired
    @Lazy
    public OpenAccessFilterDTOMapping openAccessFilterDtoMapping;


    /**
    * 创建Create 开放接入
    * 
    *
    * @param dto dto
    * @return ResponseEntity<OpenAccessDTO>
    */
    @ApiOperation(value = "创建Create", tags = {"开放接入" },  notes = "OpenAccess-Create ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccess-Create-all') or hasPermission(this.openAccessDtoMapping.toDomain(#dto),'uaa-OpenAccess-Create')")
    @PostMapping(value = {"open_accesses","open_accesses"})
    public ResponseEntity<OpenAccessDTO> create
            (@Validated @RequestBody OpenAccessDTO dto) {
        OpenAccess domain = openAccessDtoMapping.toDomain(dto);
        openAccessService.create(domain);
        OpenAccess rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(openAccessDtoMapping.toDto(rt));
    }

    /**
    * 获取Get 开放接入
    * 
    *
    * @param id id
    * @return ResponseEntity<OpenAccessDTO>
    */
    @ApiOperation(value = "获取Get", tags = {"开放接入" },  notes = "OpenAccess-Get ")
    @PostAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccess-Get-all') or hasPermission(this.openAccessDtoMapping.toDomain(returnObject.body),'uaa-OpenAccess-Get')")
    @GetMapping("open_accesses/{id}")
    public ResponseEntity<OpenAccessDTO> getById
            (@PathVariable("id") String id) {
        OpenAccess rt = openAccessService.get(id);
        return ResponseEntity.status(HttpStatus.OK).body(openAccessDtoMapping.toDto(rt));
    }

    /**
    * 删除Remove 开放接入
    * 
    *
    * @param id id
    * @return ResponseEntity<Boolean>
    */
    @ApiOperation(value = "删除Remove", tags = {"开放接入" },  notes = "OpenAccess-Remove ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccess-Remove-all') or hasPermission(this.openAccessService.get(#id),'uaa-OpenAccess-Remove')")
    @DeleteMapping("open_accesses/{id}")
    public ResponseEntity<Boolean> removeById
            (@PathVariable("id") String id) {
        Boolean rt = openAccessService.remove(id);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 更新Update 开放接入
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<OpenAccessDTO>
    */
    @ApiOperation(value = "更新Update", tags = {"开放接入" },  notes = "OpenAccess-Update ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccess-Update-all') or hasPermission(this.openAccessService.get(#id),'uaa-OpenAccess-Update')")
    @VersionCheck(entity = "openaccess" , versionfield = "updateTime")
    @PutMapping("open_accesses/{id}")
    public ResponseEntity<OpenAccessDTO> updateById
            (@PathVariable("id") String id, @Validated @RequestBody OpenAccessDTO dto) {
        OpenAccess domain = openAccessDtoMapping.toDomain(dto);
        domain.setId(id);
        openAccessService.update(domain);
        OpenAccess rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(openAccessDtoMapping.toDto(rt));
    }

    /**
    * 校验CheckKey 开放接入
    * 
    *
    * @param dto dto
    * @return ResponseEntity<Integer>
    */
    @ApiOperation(value = "校验CheckKey", tags = {"开放接入" },  notes = "OpenAccess-CheckKey ")
    @PostMapping("open_accesses/checkkey")
    public ResponseEntity<Integer> checkKey
            (@Validated @RequestBody OpenAccessDTO dto) {
        OpenAccess domain = openAccessDtoMapping.toDomain(dto);
        Integer rt = openAccessService.checkKey(domain);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * GetAuthorizationUri 开放接入
    * 
    *
    * @param id id
    * @return ResponseEntity<OpenAccessDTO>
    */
    @ApiOperation(value = "GetAuthorizationUri", tags = {"开放接入" },  notes = "OpenAccess-GetAuthorizationUri ")
    @GetMapping("open_accesses/{id}/getauthorizationuri")
    public ResponseEntity<OpenAccessDTO> getAuthorizationUriById
            (@PathVariable("id") String id) {
        OpenAccess rt = openAccessService.getAuthorizationUri(id);
        return ResponseEntity.status(HttpStatus.OK).body(openAccessDtoMapping.toDto(rt));
    }

    /**
    * 草稿GetDraft 开放接入
    * 
    *
    * @param dto dto
    * @return ResponseEntity<OpenAccessDTO>
    */
    @ApiOperation(value = "草稿GetDraft", tags = {"开放接入" },  notes = "OpenAccess-GetDraft ")
    @GetMapping("open_accesses/getdraft")
    public ResponseEntity<OpenAccessDTO> getDraft
            (@SpringQueryMap OpenAccessDTO dto) {
        OpenAccess domain = openAccessDtoMapping.toDomain(dto);
        OpenAccess rt = openAccessService.getDraft(domain);
        return ResponseEntity.status(HttpStatus.OK).body(openAccessDtoMapping.toDto(rt));
    }

    /**
    * 保存Save 开放接入
    * 
    *
    * @param dto dto
    * @return ResponseEntity<OpenAccessDTO>
    */
    @ApiOperation(value = "保存Save", tags = {"开放接入" },  notes = "OpenAccess-Save ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccess-Save-all') or hasPermission(this.openAccessDtoMapping.toDomain(#dto),'uaa-OpenAccess-Save')")
    @PostMapping("open_accesses/save")
    public ResponseEntity<OpenAccessDTO> save
            (@Validated @RequestBody OpenAccessDTO dto) {
        OpenAccess domain = openAccessDtoMapping.toDomain(dto);
        openAccessService.save(domain);
        OpenAccess rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(openAccessDtoMapping.toDto(rt));
    }

    /**
    * Sync 开放接入
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<OpenAccessDTO>
    */
    @ApiOperation(value = "Sync", tags = {"开放接入" },  notes = "OpenAccess-Sync ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccess-Sync-all') or hasPermission(this.openAccessDtoMapping.toDomain(#dto),'uaa-OpenAccess-Sync')")
    @PostMapping("open_accesses/{id}/sync")
    public ResponseEntity<OpenAccessDTO> syncById
            (@PathVariable("id") String id, @Validated @RequestBody OpenAccessDTO dto) {
        OpenAccess domain = openAccessDtoMapping.toDomain(dto);
        domain.setId(id);
        OpenAccess rt = openAccessService.sync(domain);
        return ResponseEntity.status(HttpStatus.OK).body(openAccessDtoMapping.toDto(rt));
    }

    /**
    * 查询FetchCompatible 开放接入
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<OpenAccessDTO>>
    */
    @ApiOperation(value = "查询FetchCompatible", tags = {"开放接入" },  notes = "OpenAccess-FetchCompatible ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccess-FetchCompatible-all') or hasPermission(#dto,'uaa-OpenAccess-FetchCompatible')")
    @PostMapping("open_accesses/fetchcompatible")
    public ResponseEntity<List<OpenAccessDTO>> fetchCompatible
            (@Validated @RequestBody OpenAccessFilterDTO dto) {
        OpenAccessSearchContext context = openAccessFilterDtoMapping.toDomain(dto);
        Page<OpenAccess> domains = openAccessService.searchCompatible(context) ;
        List<OpenAccessDTO> list = openAccessDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }

    /**
    * 查询FetchDefault 开放接入
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<OpenAccessDTO>>
    */
    @ApiOperation(value = "查询FetchDefault", tags = {"开放接入" },  notes = "OpenAccess-FetchDefault ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccess-FetchDefault-all') or hasPermission(#dto,'uaa-OpenAccess-FetchDefault')")
    @PostMapping("open_accesses/fetchdefault")
    public ResponseEntity<List<OpenAccessDTO>> fetchDefault
            (@Validated @RequestBody OpenAccessFilterDTO dto) {
        OpenAccessSearchContext context = openAccessFilterDtoMapping.toDomain(dto);
        Page<OpenAccess> domains = openAccessService.searchDefault(context) ;
        List<OpenAccessDTO> list = openAccessDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }


    /**
    * 批量新建开放接入
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccess-Create-all')")
    @ApiOperation(value = "批量新建开放接入", tags = {"开放接入" },  notes = "批量新建开放接入")
	@PostMapping("open_accesses/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<OpenAccessDTO> dtos) {
        openAccessService.createBatch(openAccessDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量删除开放接入
    * @param ids ids
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccess-Remove-all')")
    @ApiOperation(value = "批量删除开放接入", tags = {"开放接入" },  notes = "批量删除开放接入")
	@DeleteMapping("open_accesses/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        openAccessService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量更新开放接入
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccess-Update-all')")
    @ApiOperation(value = "批量更新开放接入", tags = {"开放接入" },  notes = "批量更新开放接入")
	@PutMapping("open_accesses/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<OpenAccessDTO> dtos) {
        openAccessService.updateBatch(openAccessDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量保存开放接入
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccess-Save-all')")
    @ApiOperation(value = "批量保存开放接入", tags = {"开放接入" },  notes = "批量保存开放接入")
	@PostMapping("open_accesses/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<OpenAccessDTO> dtos) {
        openAccessService.saveBatch(openAccessDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量导入开放接入
    * @param config 导入模式
    * @param ignoreError 导入中忽略错误
    * @return ResponseEntity<ImportResult>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccess-Save-all')")
    @ApiOperation(value = "批量导入开放接入", tags = {"开放接入" },  notes = "批量导入开放接入")
	@PostMapping("open_accesses/import")
    public ResponseEntity<ImportResult> importData(@RequestParam(value = "config" , required = false) String config ,@RequestParam(value = "ignoreerror", required = false, defaultValue = "true") Boolean ignoreError ,@RequestBody List<OpenAccessDTO> dtos) {
        return  ResponseEntity.status(HttpStatus.OK).body(openAccessService.importData(config,ignoreError,openAccessDtoMapping.toDomain(dtos)));
    }

}
